# -*- coding: utf-8 -*-
import sys
# import pixivpy_async.sync
# from pixivpy_async import AppPixivAPI
from pixivpy3 import AppPixivAPI
# from pixivpy3 import ByPassSniApi

sys.dont_write_bytecode = True

# get your refresh_token, and replace _REFRESH_TOKEN
#  https://github.com/upbit/pixivpy/issues/158#issuecomment-778919084
# _TOKEN = "eJHwtsyIuaYn2yVZF9d3loxbB5oRmJkhgMMgWtNtUSs"

def appapi_ranking():
    _TOKEN = "eJHwtsyIuaYn2yVZF9d3loxbB5oRmJkhgMMgWtNtUSs"
    aapi = AppPixivAPI()
    # aapi.require_appapi_hosts(hostname="public-api.secure.pixiv.net")
    aapi.auth(refresh_token=_TOKEN)
    json_result =  aapi.illust_ranking("day")
    print(json_result)
    with open("E:/学习相关/C++/testCode/mirai-cpp-template/link_list/d_links.txt",'w+')as f:
        for illust in json_result.illusts:
            print(illust.image_urls['large'])
            f.write(str(illust.image_urls['large'])+'\n')
    print("日榜更新完毕")

    json_result =  aapi.illust_ranking("week")
    print(json_result)
    with open("E:/学习相关/C++/testCode/mirai-cpp-template/link_list/w_links.txt",'w+')as f:
        for illust in json_result.illusts:
            print(illust.image_urls['large'])
            f.write(str(illust.image_urls['large'])+'\n')
    print("周榜更新完毕")
    

    json_result =  aapi.illust_ranking("month")
    print(json_result)
    with open("E:/学习相关/C++/testCode/mirai-cpp-template/link_list/m_links.txt",'w+')as f:
        for illust in json_result.illusts:
            print(illust.image_urls['large'])
            f.write(str(illust.image_urls['large'])+'\n')
    print("月榜更新完毕")

    json_result =  aapi.illust_ranking("day_r18")
    print(json_result)
    with open("E:/学习相关/C++/testCode/mirai-cpp-template/link_list/rd_links.txt",'w+')as f:
        for illust in json_result.illusts:
            print(illust.image_urls['large'])
            f.write(str(illust.image_urls['large'])+'\n')
    print("r18日榜更新完毕")

    json_result =  aapi.illust_ranking("week_r18")
    print(json_result)
    with open("E:/学习相关/C++/testCode/mirai-cpp-template/link_list/rw_links.txt",'w+')as f:
        for illust in json_result.illusts:
            print(illust.image_urls['large'])
            f.write(str(illust.image_urls['large'])+'\n')
    print("r18周榜更新完毕")
    # print(">>> %s, origin url: %s" % (illust.title, illust.image_urls['large']))

# def _login(aapi):
#     # await aapi.login(_USERNAME, _PASSWORD)
#     aapi.login(refresh_token=_TOKEN)

# def _main(n):
#     _login(AppPixivAPI())
#     appapi_ranking(AppPixivAPI())

# if __name__ == '__main__':
#    appapi_ranking()
